package org.shangrila.app.sys.mb;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import org.shangrila.app.sys.dao.SysDao;
import org.shangrila.app.sys.eo.SysUser;
import org.shangrila.app.sys.shiro.AppEncryptService;
import org.shangrila.comm.utils.MsgsUtil;

@Named
@SessionScoped
public class SysUserPasswdMB implements Serializable {
	private static final long serialVersionUID = 1L;
	private String userno;
	private String password;
	private String newpassword1;
	private String newpassword2;
	private SysUser user;
	
	public SysUserPasswdMB(){
		user = org.shangrila.app.sys.shiro.SecUtil.getCachedShiroUser();
		userno = user.getUserno();
		password = null;
	}
	
	public String cancelAction(){
		return null;
	}	


	// 修改密码方法
	public String passwdAction() {
		// 验证2次输入一致
		if (!newpassword1.equals(newpassword2)) {
			MsgsUtil.showMessages("错误提示","两次输入密码不一致。");
			return null;
		}

		// 验证旧密码输入正确性		
		SysDao sysdao = new SysDao();
		try{
			SysUser user0 = (SysUser) sysdao.findSysUserByUsernoPassword(userno, AppEncryptService.encryptPassword(userno,AppEncryptService.encryptMD5(password)));
			// 将新输入的密码newpassword2进行加密后存储在数据库中
			user0.setPassword(AppEncryptService.encryptPassword(userno,AppEncryptService.encryptMD5(newpassword2)));
			user0.setSalt(AppEncryptService.encryptSalt(userno,AppEncryptService.encryptMD5(newpassword2)));
			sysdao.dbAccess.beginTransaction();
			sysdao.dbAccess.update(user0);
			sysdao.dbAccess.commitTransaction();	
			MsgsUtil.showMessages("提示","密码修改成功。");
		}catch(Exception ex){
			MsgsUtil.showMessages("错误提示","旧密码输入错误。");
		}		
				
		password = null;
		return null;
		
	}
	
	public String getUserno() {
		return userno;
	}

	public void setUserno(String userno) {
		this.userno = userno;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNewpassword1() {
		return newpassword1;
	}

	public void setNewpassword1(String newpassword1) {
		this.newpassword1 = newpassword1;
	}

	public String getNewpassword2() {
		return newpassword2;
	}

	public void setNewpassword2(String newpassword2) {
		this.newpassword2 = newpassword2;
	}

}
